package com.moorror.codegenvel.common.table;

import com.google.common.collect.Sets;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.moorror.codegenvel.common.select.MRMultiSelect;
import com.moorror.codegenvel.utils.GuiUtils;
import com.moorror.codegenvel.utils.JacksonUtils;
import org.apache.commons.lang3.tuple.Pair;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import java.awt.*;
import java.util.Set;
import java.util.function.Consumer;

/**
 * @Author jiyanghuang
 * @Date 2025/8/29 08:47
 */
public class CellEditorMultiSelect extends AbstractCellEditor implements TableCellEditor, TreeCellEditor {

    private Project project;

    private MRMultiSelect multiSelect;

    protected JPanel toolBarPanel;

    public CellEditorMultiSelect(Project project) {
        this.project = project;
        DefaultActionGroup actionGroup = new DefaultActionGroup("CellEditorGroup", false);
        Set<Pair<String, Consumer<Boolean>>> allItems = Sets.newHashSet();
        Pair<String, Consumer<Boolean>> rightPanel = Pair.of("one", (b) -> {

        });
        Pair<String, Consumer<Boolean>> lineMarkerOpen = Pair.of("two", (b) -> {

        });
        allItems.add(rightPanel);
        allItems.add(lineMarkerOpen);
        multiSelect = new MRMultiSelect(allItems, "CellEditor");
        multiSelect.getItemSelects().add("one");
        actionGroup.add(multiSelect);
        toolBarPanel = new JPanel();
        toolBarPanel.setLayout(new BorderLayout());
        GuiUtils.installActionGroupInToolBar(actionGroup, toolBarPanel, ActionManager.getInstance(), ActionPlaces.TOOLBAR, true);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return toolBarPanel;
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        return toolBarPanel;
    }

    @Override
    public Object getCellEditorValue() {
        String jsonValue = JacksonUtils.toJSONString(multiSelect.itemSelects());
        return jsonValue.substring(1, jsonValue.length() - 1);
    }
}
